/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.netbeans.modules.javawebstart;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.util.HashMap;
import java.util.Map;

import org.netbeans.modules.java.j2seproject.api.J2SECategoryExtensionProvider;
import org.netbeans.modules.javawebstart.ui.customizer.JWSProjectProperties;

/**
 * 
 * @author  Milan Kubec
 */
public class CustomizerRunComponent extends javax.swing.JPanel implements ActionListener {
    
    private Map<String,String> runSelectedMap = new HashMap<String,String>();
    private Map<String,String> runUnselectedMap = new HashMap<String,String>();
    
    private J2SECategoryExtensionProvider.ConfigChangeListener listener;
    
    public CustomizerRunComponent() {
        initComponents();
        runCheckBox.addActionListener(this);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        runCheckBox = new javax.swing.JCheckBox();
        hintLabel = new javax.swing.JLabel();

        setLayout(new java.awt.GridBagLayout());

        org.openide.awt.Mnemonics.setLocalizedText(runCheckBox, org.openide.util.NbBundle.getMessage(CustomizerRunComponent.class, "LBL_Run_with_JWS")); // NOI18N
        runCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        add(runCheckBox, gridBagConstraints);
        runCheckBox.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(CustomizerRunComponent.class, "ACSN_Run_With_JWS")); // NOI18N
        runCheckBox.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(CustomizerRunComponent.class, "ACSD_Run_With_JWS")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(hintLabel, org.openide.util.NbBundle.getMessage(CustomizerRunComponent.class, "HINT_Run_with_JWS")); // NOI18N
        hintLabel.setToolTipText(org.openide.util.NbBundle.getMessage(CustomizerRunComponent.class, "HINT_Run_with_JWS_tooltip")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 6, 0, 0);
        add(hintLabel, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents
    
    public void addListener(J2SECategoryExtensionProvider.ConfigChangeListener l) {
        listener = l;
    }
    
    public void setCheckboxEnabled(boolean b) {
        runCheckBox.setEnabled(b);
    }
    
    public void setCheckboxSelected(boolean b) {
        runCheckBox.setSelected(b);
    }
    
    public void setHintVisible(boolean b) {
        hintLabel.setVisible(b);
    }
    
    public void actionPerformed(ActionEvent e) {
        initMaps();
        if (runCheckBox.isSelected()) {
            listener.propertiesChanged(runSelectedMap);
        } else {
            listener.propertiesChanged(runUnselectedMap);
        }
    }
    
    private void initMaps() {
        runUnselectedMap.put(JWSProjectProperties.CONFIG_TARGET_RUN_PROPNAME, null);
        runUnselectedMap.put(JWSProjectProperties.CONFIG_TARGET_DEBUG_PROPNAME, null);
        runUnselectedMap.put(JWSProjectProperties.COS_UNSUPPORTED_PROPNAME, null);
        runSelectedMap.put(JWSProjectProperties.CONFIG_TARGET_RUN_PROPNAME, JWSProjectProperties.CONFIG_TARGET_RUN);
        runSelectedMap.put(JWSProjectProperties.CONFIG_TARGET_DEBUG_PROPNAME, JWSProjectProperties.CONFIG_TARGET_DEBUG);
        runSelectedMap.put(JWSProjectProperties.COS_UNSUPPORTED_PROPNAME, "true"); // NOI18N
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel hintLabel;
    private javax.swing.JCheckBox runCheckBox;
    // End of variables declaration//GEN-END:variables
    
}
